package com.aier.controller.inventory;

import com.aier.bean.inventory.ProductEntryBean;
import com.aier.bean.common.CommonResult;
import com.aier.service.inventory.InventoryService;
import com.aier.service.inventory.ProductEntryService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Map;

@Controller
public class ProductController {

    @Resource
    private InventoryService inventoryService;

    @Resource
    private ProductEntryService productEntryService;

    @RequestMapping("")
    public String start(){
        return "enterProduct.html";
    }

    @RequestMapping("/enter_product")
    @ResponseBody
    public CommonResult enterProduct(@RequestBody ProductEntryBean productEntryBean){
        if(productEntryBean.getEnterDate() == null){
            productEntryBean.setEnterDate(LocalDate.now());
        }
        inventoryService.getAllInventoryInfoByModelName(productEntryBean.getModelName());
        inventoryService.inventoryIsExistByModelNameAndSalesperson(productEntryBean.getModelName(),productEntryBean.getSalesperson());
        return productEntryService.saveProductEntry(productEntryBean);
    }

    @RequestMapping("/update_salesperson")
    @ResponseBody
    @Transactional
    public CommonResult updateSalesperson(@RequestBody Map<String,String> resultMap){
        inventoryService.updateSalespersonByModelNameAndOldSalesperson(resultMap.get("modelName"), resultMap.get("oldSalesperson"), resultMap.get("newSalesperson"));
        productEntryService.updateSalespersonByModelNameAndOldSalesperson(resultMap.get("modelName"), resultMap.get("oldSalesperson"), resultMap.get("newSalesperson"));
        return CommonResult.success();
    }


}
